#include "sql_fundlist.h"
#include <stdio.h>
#include <string.h>

#include <QDebug>

SQL_FundList::SQL_FundList()
{
    sqlListData = new basic_sql_data();
    sqlListData->sqldb = QSqlDatabase::addDatabase("QSQLITE");

    sqlListData->sqldb.setDatabaseName("./fundList_sql.db");
    if(sqlListData->sqldb.open()) {
        sqlListData->query = new QSqlQuery(sqlListData->sqldb);

        // Create anyway
        sqlListData->query->exec(QObject::tr("CREATE TABLE fund_List (\
                               code VARCHAR(6) primary key not null, \
                               fund_name TEXT not null)"));
        qDebug() << "Fund list sql open success" << endl;

//        qDebug() << "insert rslt " << insert_by_code_and_name("000002", "owow") << endl;
//        qDebug() << "insert rslt " << insert_by_code_and_name("000003", "ksks") << endl;

//        sqlListQueue = new QueueList();

        updateSqlQueue();
    } else {
        qDebug() << "Fund list Sql open failed" << endl;
    }
}

SQL_FundList::~SQL_FundList() {
    if(sqlListData->sqldb.isOpen()) {
        sqlListData->sqldb.close();
        delete sqlListData->query;
//        delete sqlListQueue;
    }
    delete sqlListData;
}

int SQL_FundList::insert_by_code_and_name(QString code, QString name) {
    if(sqlListData->sqldb.isOpen()) {
        if(code == NULL || name == NULL) {
            return (-2);
        }
        const char *cmd = QString2char("insert into fund_List values (\"" + code + "\", \"" + name + "\")");
        bool rslt = sqlListData->query->exec(QObject::tr(cmd));
        delete cmd;
        if(rslt) {
            qDebug() << "fund list insert success" << endl;
            sql_fundListData fundList_Node;// = new sql_fundListData();
            fundList_Node.code = code;
            fundList_Node.name = name;
//            sqlListQueue->append(fundList_Node);
            sqlList.append(fundList_Node);
            return 0;
        } else {
            qDebug() << "fund list insert failed" << endl;
            return (-3);
        }
    }
    return (-1);
}

bool SQL_FundList::delete_by_code(QString code) {
    // qDebug() << "delete code is " << code << endl;
    if(sqlListData->sqldb.isOpen()) {
        if(code == NULL) {
            return false;
        }
        const char *cmd = QString2char("DELETE FROM fund_List WHERE code=\"" + code + "\"");
        bool rslt = sqlListData->query->exec(QObject::tr(cmd));
        delete cmd;
        if(rslt) {
            for(int i = 0; i < sqlList.size(); i++) {
                sql_fundListData fundList_Node = sqlList.at(i);
                if(fundList_Node.code.compare(code) == 0) {
                    sqlList.removeAt(i);
                    return true;
                }
            }
        } else {
            return false;
        }
    }
    return false;
}

void SQL_FundList::updateSqlQueue() {
    if(sqlListData->sqldb.isOpen()) {
        bool rslt = sqlListData->query->exec(QObject::tr("SELECT * FROM fund_List"));
        if(rslt) {
            while(sqlListData->query->next()) {
                // This is not necessary because the index is stable.
                QSqlRecord rec = sqlListData->query->record();
                int code_index = rec.indexOf("code");
                int fund_index = rec.indexOf("fund_name");
                sql_fundListData fundList_Node;// = new sql_fundListData();
                fundList_Node.code = sqlListData->query->value(code_index).toString();
                fundList_Node.name = sqlListData->query->value(fund_index).toString();
                sqlList.append(fundList_Node);
            }
        }
    } else {
        qDebug() << "fund list sql not opened" << endl;
    }
}

int SQL_FundList::get_fundList_count() {
//    return sqlListQueue->getSize();
    return sqlList.size();
//    return 0;
}

sql_fundListData SQL_FundList::get_fundList_by_index(int index) {
//    sql_fundListData *prslt;
    sql_fundListData rslt;
    rslt.code = nullFundCode;
    rslt.name = nullFundName;

//    qDebug() << "get fundlist index now is " << index << " total count is " << get_fundList_count() << endl;

    if(index >= 0 && index < get_fundList_count()) {
//        prslt = (sql_fundListData*)sqlListQueue->get(index);

//        rslt.code = prslt->code;
//        rslt.name = prslt->name;
//        qDebug() << "Get fund code " << prslt->code << endl;
//        rslt = *(sql_fundListData*)sqlListQueue->get(index);
//        rslt.code = *(QString *)(sqlListQueue->get(index).code);
//        rslt.code = *(QString *)sqlListQueue->get(index);
        rslt = sqlList.at(index);
    }

    return rslt;
}

bool SQL_FundList::checkCode(QString code) {
    const char *cmd = QString2char("SELECT * FROM fund_List WHERE code=\"" + code + "\"");
    bool rslt = sqlListData->query->exec(QObject::tr(cmd));
    if(rslt) {
//        qDebug() << "checkCode " << sqlListData->query->next();
        return sqlListData->query->next();
    }
    return true;
}
